package wnhz.com.business.groupshop.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.groupshop.bean.MyWalletBean;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.netcallback.DialogCallback;
import wnhz.com.business.rebateshop.activity.mine.RechargeActivity;
import wnhz.com.business.uitls.Base64Util;
import wnhz.com.business.uitls.ToastUtils;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.view.TranslucentActionBar;

public class FundActivity extends BaseActivity implements View.OnClickListener, ActionBarClickListener {

    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.tv_huangouquan)
    TextView tv_huangouquan;
    @BindView(R.id.tv_duihuanbi)
    TextView tvDuihuanbi;
    @BindView(R.id.tv_xianjinjuan)
    TextView tvXianjinjuan;
    @BindView(R.id.tv_duidou)
    TextView tvDuidou;
    @BindView(R.id.ll_head)
    LinearLayout llHead;
    @BindView(R.id.ll_tixian)
    LinearLayout llTixian;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.ll_mycard)
    LinearLayout llMycard;
    @BindView(R.id.ll_chongzhi)
    LinearLayout llChongzhi;
    @BindView(R.id.tv_wallet_overage)//余额
    TextView tvWalletOverage;


    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, FundActivity.class);
        return intent;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fund);
        ButterKnife.bind(this);
        actionbar.setData("我的钱包", R.drawable.ic_lefft_white, null, 0, "交易记录", 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        loadData();
    }

    private void loadData() {
        Map<String, String> map = new HashMap<>();
        if (MyApplication.getInstance().getInfoBean() == null) {
            MyApplication.getInstance().BackLoginActivity();
            return;
        } else {
            map.put("token", MyApplication.getInstance().getInfoBean().getToken());
        }
        OkGo.<String>post(Url.UCENTER_MYWALLET)
                .tag(this)
                .params(map, false)
                .execute(new DialogCallback<String>(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.e("我的钱包", response.body().toString());
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(response.body().toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String re = jsonObject.optString("re");
                        if (re.equals("1")) {
                            Gson gson = new Gson();
                            MyWalletBean walletBean = gson.fromJson(response.body(), MyWalletBean.class);
                            tvWalletOverage.setText(Base64Util.decodedString2(walletBean.getBalance()));
                            tv_huangouquan.setText(Base64Util.decodedString2(walletBean.getCoupon()));
                            tvDuihuanbi.setText(Base64Util.decodedString2(walletBean.getCurrency()));
                            tvXianjinjuan.setText(Base64Util.decodedString2(walletBean.getCash()));
                            tvDuidou.setText(Base64Util.decodedString2(walletBean.getBeans()));
                        } else {
                            ToastUtils.showToast(FundActivity.this, "获取信息失败");
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        }
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {
        Intent intent = new Intent(FundActivity.this,JiaoYiRecordActivity.class);
        startActivity(intent);
    }

    @Override
    public void onEditTextClick() {

    }

    @OnClick({R.id.ll_tixian, R.id.ll_mycard, R.id.ll_chongzhi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_tixian://提现
                startActivity(TiXianActivity.createIntent(this));
                break;
            case R.id.ll_mycard://我的银行卡
                break;
            case R.id.ll_chongzhi://充值
                startActivity(RechargeActivity.createIntent(this));
                break;
        }
    }
}
